/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package fts_test;

import fts_test.lib.*;
import iboxdb.localserver.*;
import java.io.*;

import java.text.NumberFormat;
import java.util.concurrent.ConcurrentLinkedQueue;
import org.apache.lucene.document.Document;

public class Fts_test {

    public static void main(String[] args) throws Exception {

        String root = System.getProperty("user.home") + "/fts_test_data/";
        root = "../fts_test_data/";
        System.out.println(new File(root).getAbsolutePath());
        String iboxdb_root = root + "ftserver/";
        String lucene_root = root + "lucene";
        String book = "libs/175315.txt"; //<<石头记>>, must UTF-8 Book

        new File(iboxdb_root).mkdirs();
        new File(lucene_root).mkdirs();

        for (File f : new File(iboxdb_root).listFiles()) {
            f.delete();
        }
        for (File f : new File(lucene_root).listFiles()) {
            f.delete();
        }

        // JDK 1.8
        System.out.println("FTS Speed " + System.getProperty("java.version") + " , "
                + java.lang.Runtime.getRuntime().maxMemory());

        int searchTime = 20000;
        String[] searchtxts = new String[]{
            "二仙师 听 毕",
            "林黛玉",
            "士隐 雨村",
            "果然有 曹雪芹先 生笑道",
            "大哥前 待此纸",
            "八方宁静 千载 百载不用武备",
            "槛外之人 合了他的心"

        };
        int searchTopn = Integer.MAX_VALUE;

        System.out.println("建索引...");
        {
            long begin = System.currentTimeMillis();

            String[] ts = iboxdbfts.readbook(iboxdb_root, book);

            System.out.println("iBoxDB: " + ((System.currentTimeMillis() - begin) / 1000.0) + "s , 总数" + ts.length);
        }
        {
            long begin = System.currentTimeMillis();

            String[] ts = lucene.readbook(lucene_root, book);

            System.out.println("Lucene: " + ((System.currentTimeMillis() - begin) / 1000.0) + "s , 总数" + ts.length);

        }

        Object[] iboxdbargs = iboxdbfts.searchbook_engine(iboxdb_root);
        Object[] luceneargs = lucene.searchbook_engine(lucene_root);

        System.out.println("");
        System.out.println("搜索准确度测试 ");
        {
            for (String searchtxt : searchtxts) {

                System.out.println("");
                ConcurrentLinkedQueue<Local> iboxdb_list
                        = iboxdbfts.searchbook(iboxdbargs, 1, searchTopn, searchtxt);
                System.out.println("iBoxDB 搜索 " + searchtxt + " 结果总数: " + iboxdb_list.size());

                ConcurrentLinkedQueue<Document> lucene_list
                        = lucene.searchbook(luceneargs, 1, searchTopn, searchtxt);
                System.out.println("Lucene 搜索 " + searchtxt + " 结果总数: " + lucene_list.size());

                if (iboxdb_list.size() < 10 && lucene_list.size() < 10) {
                    System.out.println("搜索 " + searchtxt + " 结果");

                    System.out.print("iBoxDB:");
                    if (iboxdb_list.isEmpty()) {
                        System.out.println("没有数据");
                    } else {
                        System.out.println("");
                        for (Local l : iboxdb_list) {
                            System.out.println(l.get("info").toString().trim());
                            System.out.println("---------------------");
                        }
                    }
                    System.out.print("Lucene:");
                    if (lucene_list.isEmpty()) {
                        System.out.println("没有数据");
                    } else {
                        System.out.println("");
                        for (Document l : lucene_list) {
                            System.out.println(l.get("info").toString().trim());
                            System.out.println("---------------------");
                        }

                    }
                }
            }
        }

        System.out.println("");
        System.out.println("搜索速度测试 ");
        {
            long begin = System.currentTimeMillis();
            for (String searchtxt : searchtxts) {
                iboxdbfts.searchbook(iboxdbargs, searchTime, searchTopn, searchtxt);
            }

            System.out.println("iBoxDB: " + NumberFormat.getInstance().format((System.currentTimeMillis() - begin) / 1000.0)
                    + "s 搜索 " + searchTime * searchtxts.length + " 次");
        }
        iboxdbfts.searchbook_engine_close(iboxdbargs);

        {
            long begin = System.currentTimeMillis();
            for (String searchtxt : searchtxts) {
                lucene.searchbook(luceneargs, searchTime, searchTopn, searchtxt);
            }
            System.out.println("Lucene: " + NumberFormat.getInstance().format((System.currentTimeMillis() - begin) / 1000.0)
                    + "s 搜索 " + searchTime * searchtxts.length + " 次");

        }
        lucene.searchbook_engine_close(luceneargs);

        System.out.println("End.");

    }

}
